package com.jdragon.tljrobot.client.utils.common;

import com.jdragon.tljrobot.client.config.LocalConfig;
import com.jdragon.tljrobot.client.event.FArea.QQGetArticleEvent;
import com.jdragon.tljrobot.client.utils.Local;
import com.jdragon.tljrobot.client.utils.SystemUtil;
import com.jdragon.tljrobot.client.window.MainFra;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import lombok.extern.log4j.Log4j2;

import java.awt.*;

import static java.awt.event.KeyEvent.*;

@Log4j2
public class QqOperation {

    public static final int GET_ARTICLE = 1;
    public static final int SEND_ACHIEVEMENT = 2;

    public static void start(int action, String name) throws Exception {
        Robot robot = new Robot();
        switchToQqWindow(robot, name);
        robot.delay(500);


        if (GET_ARTICLE == action) {
            Clipboard.set(LocalConfig.command);
            sendClipboardText(robot);

            // 等待机器人回复
            robot.delay(2000);

            copyMessage(robot);
        } else if (SEND_ACHIEVEMENT == action) {
            sendClipboardText(robot);
        }

        robot.delay(1000);
        if (SystemUtil.isWindows()) {
            HWND genda = User32.INSTANCE.FindWindow(null, MainFra.getInstance().getTitle());
            User32.INSTANCE.SetForegroundWindow(genda);
        } else {
            keyboardAction(robot, SystemUtil.isMac() ? VK_META : VK_ALT, VK_TAB);
        }

        if (action == GET_ARTICLE) {
            MainFra mainFrame = MainFra.getInstance();
            robot.mouseMove(mainFrame.getX() + mainFrame.getWidth() / 2, mainFrame.getY() + mainFrame.getHeight() / 2);
            robot.mousePress(BUTTON1_MASK);
            robot.mouseRelease(BUTTON1_MASK);
            robot.delay(2000);
            // keyboardAction(robot, VK_CONTROL, VK_E);
            QQGetArticleEvent.isGetArticleSign = true;
        }
    }

    public static void copyMessage(Robot robot) {
        robot.mouseMove(LocalConfig.messagePositionX, LocalConfig.messagePositionY);
        robot.mousePress(BUTTON3_DOWN_MASK);
        robot.mouseRelease(BUTTON3_DOWN_MASK);
        robot.delay(50);
        robot.mouseMove(LocalConfig.messagePositionX + 20, LocalConfig.messagePositionY + 10);
        robot.mousePress(BUTTON1_MASK);
        robot.mouseRelease(BUTTON1_MASK);
    }

    private static void focusInput(Robot robot) {
        keyboardAction(robot, VK_TAB);
        keyboardAction(robot, VK_BACK_SPACE);
    }

    public static void switchToQqWindow(Robot robot, String name) {
        if (SystemUtil.isWindows()) {
            HWND hWnd = User32.INSTANCE.FindWindow("TXGuiFoundation", name);
            if (hWnd != null) {
                //切换到聊天窗口
                User32.INSTANCE.SetForegroundWindow(hWnd);
            }
        } else {
            keyboardAction(robot, LocalConfig.qqShortCut);
        }
    }

    public static void sendClipboardText(Robot robot) {
        // 切换到输入框
        focusInput(robot);

        // 粘贴
        robot.delay(200);
        keyboardAction(robot, SystemUtil.isMac() ? VK_META : VK_CONTROL, VK_V);

        // Enter
        robot.delay(200);
        if (LocalConfig.ctrlSendAchToQQ) {
            keyboardAction(robot, VK_CONTROL, VK_ENTER);
        } else {
            keyboardAction(robot, VK_ENTER);
        }
    }

    private static void keyboardAction(Robot robot, int... keys) {
        for (int key : keys) {
            robot.keyPress(key);
            robot.delay(100);
        }
        for (int key : keys) {
            robot.keyRelease(key);
        }
    }
}
